import "./public-path";
import Vue from "vue";
import App from "./App.vue";
import routes from "./router";
import store from "./store";
import VueRouter from "vue-router";
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
import "./assets/style/element-variables.scss";
// import '@/assets/font/fonts.scss';
import "normalize.css/normalize.css";

import filters from "@/filter";
Object.keys(filters).forEach((k) => Vue.filter(k, filters[k]));

import httpRequest from "@/api";
Vue.prototype.$Ajax = httpRequest.Ajax;

Vue.config.productionTip = false;

window.echarts = require('echarts')

Vue.use(ElementUI);

import Upload from "@/components/Upload";
Vue.component("Upload", Upload);

import Image from "@/components/Image";
Vue.component("Images", Image);

import Search from "@/components/Search";
Vue.component("Search", Search);

import MainHead from "@/components/Layout/main/header";
Vue.component("MainHead", MainHead);

import Popconfirm from "@/components/Popconfirm";
Vue.component("Popconfirm", Popconfirm);

let instance = null;
let router = null;

function render (props = {}) {
  const { container } = props;
  router = new VueRouter({
    mode: "hash",
    routes,
  });

  router.beforeEach((to, from, next) => {
    const historyRouter = store.state.historyRouter;
    // const token = store.state.token
    // if (token && Object.values(store.state.userInfo).length === 0) {
    //   httpRequest.Ajax(
    //   '/accountUser/getMyUser',
    //   'get',
    //   {},
    //   (res) => {
    //     store.commit("SET_USER_INFO", res.data);
    //   }
    //   )
    // }
    if (historyRouter) {
      const historyPath = historyRouter.map((item) => item.path);
      if (!historyPath.includes(to.path) && to.path !== "/") {
        historyRouter.push({
          path: to.path,
          title: to.meta.title,
          query: to.query,
        });
      }
    }
    console.log(1)
    store.commit("SET_HIST_ROUTER", historyRouter);

    document.title = "党建信息化管理系统 - " + to.meta.title;
    // const userId = store.state.userInfo.userId
    // if (to.meta.isLogin) {
    //   if (userId) {
    //     const isAuth = to.meta.isAuth || false
    //     const userMenu = store.state.menu.userMenuAuth || []
    //     const arr = userMenu.filter(item => ('/' + item.linkUrl) === to.path && item.isShow === 1)
    //     if (arr.length > 0) {
    //       const currRouterInfo = arr[0]
    //       const permissions = currRouterInfo.permissions || ''
    //       to.meta.btnAuthList = permissions.split(',')
    //     }
    //     if (!isAuth || arr.length > 0) {
    //       next()
    //     } else {
    //       next({ path: '401' })
    //     }
    //   } else {
    //     next({ path: '/' })
    //   }
    // } else {
    next();

    // }
  });
  instance = new Vue({
    router,
    store,
    render: (h) => h(App),
  }).$mount(container ? container.querySelector("#app") : "#app");
}

// 独立运行时
if (!window.__POWERED_BY_QIANKUN__) {
  render();
}

export async function bootstrap () {
  console.log("[vue] vue app bootstraped");
}
export async function mount (props) {
  console.log("[vue] props from main framework", props);
  render(props);
}

export async function unmount () {
  instance.$destroy();
  instance.$el.innerHTML = "";
  instance = null;
  router = null;
}

export default function goLogin () {
  router.replace('/')
}